﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Paperless.Entities;
using Paperless.Repositories;
using Resources;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Paperless.Controllers
{
    //[Authorize]
    public class WorkingController : Controller
    {
        public IRepository<Document> Repository { get; set; }
        public UserManager<User> UserManager { get; private set; }

        public WorkingController(IRepository<Document> repository, UserManager<User> userManager)
        {
            Repository = repository;
            UserManager = userManager;
        }

        //
        // GET: /Working/
        public ActionResult Index()
        {
            
            return View();
        }

        public ActionResult Upload()
        {
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> SaveUploadedFile()
        {
            bool isSavedSuccessfully = true;
            string fName = "";
            string userId = User.Identity.GetUserId();

            var user = await UserManager.FindByIdAsync(userId);

            foreach (string fileName in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[fileName];
                //Save file content goes here
                fName = file.FileName;
                var type = file.ContentType;
                if (file != null && file.ContentLength > 0)
                {
                    string extension = Path.GetExtension(file.FileName);

                    var originalDirectory = new DirectoryInfo(string.Format("{0}Files", Server.MapPath(@"\")));

                    string pathString = originalDirectory.ToString();

                    var oldFileName = Path.GetFileName(file.FileName);

                    string newFileName = Guid.NewGuid().ToString() + extension;



                    Document document = new Document()
                    {
                        Name = oldFileName,
                        Path = newFileName,
                        CreatedBy = userId,
                        CreatedDate = DateTime.UtcNow,
                        Id = 0,
                        LocationId = user.LocationId
                    };

                    var task = Repository.Add(document);
                    task.Wait();
                    int documentId = task.Result;

                    if (documentId > 0)
                    {
                        bool isExists = System.IO.Directory.Exists(pathString);

                        if (!isExists)
                            System.IO.Directory.CreateDirectory(pathString);

                        var path = string.Format("{0}\\{1}", pathString, newFileName);
                        file.SaveAs(path);
                    }

                }

            }

            if (isSavedSuccessfully)
            {
                return Json(new { Message = fName });
            }
            else
            {
                return Json(new { Message = "Error in saving file" });
            }
        }
    }
}